package server;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;

public class AceptarClientes extends Thread{

	private ServerSocket socket;
	private Server server;
	
	public AceptarClientes(ServerSocket socket,Server server){
		this.server=server;
		this.socket=socket;
	}
	
	public ServerSocket getSocket() {
		return socket;
	}

	public void setSocket(ServerSocket socket) {
		this.socket = socket;
	}

	public Server getServer() {
		return server;
	}

	public void setServer(Server server) {
		this.server = server;
	}

	@Override
	public void run(){
		while(true){
			try {
				Socket s=this.getSocket().accept();
				s.getInputStream();
				ObjectInputStream d=new ObjectInputStream(s.getInputStream() );
				try {
				Hashtable<String,String> info=(Hashtable<String,String>)d.readObject();
				    String nombre=info.get("nombre");
				    String apellido=info.get("apellido");
				    String nick=info.get("nick");
				    String ip=info.get("ip");
					server.agregar(new Usuario(nombre,apellido,nick,ip));
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					System.out.println("Ocurrio un error al leer socket");
				}
			} catch (IOException e) {
				System.out.println("Ocurrio un error");
			}
		}
	}
}
